<template>
	<view>
		<view class="editAll" style="background-color: gainsboro;">
			
			<button class="addmoban"  @click="sendmoban()">添加模版</button>
			<input type="text" v-model="inputtext" />
			<button class="addactbtn" @click="open()">添加动作</button>
			
	        <view>
				<view class="" style="display: flex;justify-content: space-between;padding-bottom: 30rpx;">
					<view class="" @click="toxunlian()" style="padding-left: 20rpx;font-size: 35rpx;">
						返回
					</view>
					<view class="" style="font-size: 35rpx;">
						模版编辑
					</view>
					<view class="" style="color: blue;font-size: 35rpx;padding-right: 20rpx;">
						保存
					</view>
				</view>
			    <input class="inputmoban" type="text" v-model="addList.mouldname"  placeholder="模版名称"/>
                        <!-- <view class="mobanname">
                        	{{addList.mouldname}}
                        </view> -->
				 <view class="Agroup" v-for="(item,index) in addList.groups" :key="index" style="">
                   <h3>{{index+1}}	{{item.name}}</h3>
					<br />
					
					<view class="minigroup" v-for="(item1,index1) in addList.groups[index].miniGroups" :key="index1">
						<view style="padding: 10rpx;background-color: #f5f5f5;border-radius: 15%;height: 50rpx;">{{index1+1}}</view>
						<input type="text" v-model="item1.weight"/>kg
						<input type="text" v-model="item1.number"/>次
						<button @click="delmini(index,index1)">删除</button>
					</view>
					<view class="caozuo" style="margin-top: 10rpx;">
						<el-button @click="addmini(index)">新增一组</el-button>
						<el-button @click="delgroup(index)">删除该组</el-button>
					</view>
                 </view>
			</view>
		</view>
		<uni-popup ref="popup" type="center">
			  <view class="pop">

					<!-- <el-button @click="test()">test</el-button> -->
						<!-- <el-button @click="getact">获取动作</el-button> -->
			  <scroll-view  scroll-y >
		        <h2>请选择动作</h2>
				    <el-select
				      v-model="groupmmban.name"
				      placeholder="Select"
				      size="large"
				      style="width: 240px"
				    >
				      <el-option
				        v-for="item in actlist"
				        :key="item.name"
				        :label="item.name"
				        :value="item.name"
				      />
				    </el-select>
			  	</scroll-view>
			<el-button @click="close()" >完成</el-button>
			  
			 
			  </view>
			  
			</uni-popup>
	</view>
</template>

<script>
	class group{
		constructor(arg) {
		    this.arg=arg
		}
	}
	export default {
		data() {
			return {
				mobanlength:'',
				
				inputtext:'',
				inputlist:[],
				actlist:[],
				addList:{
					    createtime: "1979-08-31 21:58:37",
					    status: 1,
					    mouldid: 10,
					    groups:  [],
					    mouldname: "",
					    userid: 1,
					},
					groupmmban:{
						    id:'' ,
						    order: '1',
						    actid: '',
						    statu: '1',
						    point: "dolore sit Duis labore culpa",
						    restspan: '20',
						    name: "",
						    miniGroups: [
						    
						    ]
					},
					miniGroupsmoban:
						{
						    order:'' ,
						    weight:'' ,
						    restspan:'10' ,
						    status:'1' ,
						    groupid: '',
						    span:'2' ,
						    id: '',
						    number: ''
						}
					
				
			}
		},
		onLoad() {
			this.getact()
			this.getmoban()
		},
		methods: {
			gettime(){
				const date = new Date();
				const year = date.getFullYear();
				const month = (date.getMonth() + 1).toString().padStart(2, '0');
			 const day = date.getDate().toString().padStart(2, '0');
			 const hour = date.getHours().toString().padStart(2, '0');
			 const minute = date.getMinutes().toString().padStart(2, '0');
			 const second = date.getSeconds().toString().padStart(2, '0');
		    const currentTime = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
		    
				return currentTime
			},
			getactid(name){
				for(let a=0;a<this.actlist.length;a++)
				{
					if(this.actlist[a].name===name)
					{
						this.groupmmban.actid=this.actlist[a].id
					}
				}
			},
			deepCopy(obj) {
			  return JSON.parse(JSON.stringify(obj));
			},
			delgroup(index){
				console.log("del success");
				this.addList.groups.splice(index,1)
			},
			addzu(){
				console.log(this.groupmmban)
	// 			const newgr={
	// 				            id: 2,
	// 				            order: 2,
	// 				            actid: 1,
	// 				            statu: 20,
	// 				            point: "dolore sit Duis labore culpa",
	// 				            restspan: 60,
	// 				            name: "暂停卧推",
	// 				            miniGroups: [
	// 				                {
	// 				                    order: 1,
	// 				                    weight: 30,
	// 				                    restspan: 67,
	// 				                    status: 91,
	// 				                    groupid: 95,
	// 				                    span: 75,
	// 				                    id: 50,
	// 				                    number: 71
	// 				                },
 
	// 				            ]
	// 				        }
			   	this.getactid(this.groupmmban.name)
				this.groupmmban.order=this.addList.groups.length+1
				this.groupmmban.id=this.addList.groups.length+1
				let a = this.deepCopy(this.groupmmban)
				this.addList.groups.push(a)
				console.log(this.addList)
			},
			addmini(index){
				// const index=
				this.miniGroupsmoban.id=this.addList.groups[index].miniGroups.length+1
				this.miniGroupsmoban.order=this.addList.groups[index].miniGroups.length+1
				this.miniGroupsmoban.groupid=index+1
				console.log(this.miniGroupsmoban.groupid);
				// console.log(this.miniGroupsmoban.order);
				let a=this.deepCopy(this.miniGroupsmoban)
				
				this.addList.groups[index].miniGroups.push(a)
			},
			delmini(index,miniindex){
				this.addList.groups[index].miniGroups.splice(miniindex,1)
			},
			test(){
				this.addList.createtime=this.gettime()
				console.log(this.addList.createtime)
				console.log(this.gettime())
			},
			getmoban(){
										  uni.request({
										  	url:'http://localhost:8080/mould/select',
											method:'POST',
											 data:{
												 id:1
												 },
												 header:{'content-type':'application/x-www-form-urlencoded'},
											 success: (res) => {
											 	console.log(res)
												this.mobanlength=res.data.data.length
												console.log(this.mobanlength)
											 }
										  })
			},
			sendmoban(){
				this.addList.mouldid=this.mobanlength+1
				this.addList.createtime=this.gettime()
				uni.request({
					url:'http://localhost:8080/mould/add',
					method:'POST',
					data:this.addList,
					header:{
						'content-type':'application/json'
					},
					success:(res)=>{
						console.log(res.data)
						alert("添加成功")
						uni.switchTab({
							
							url:'/pages/tabbar/xunlian/xunlian'
						})
					}
				})
					
				
			},
			getact(){
				uni.request({
					url:'http://localhost:8080/act/selallact',
					method:'GET',
					success:(res) =>{
					 console.log(res.data);
					 this.actlist=res.data.data
					 console.log(this.actlist);
					}
				})
			},
			toxunlian(){
				uni.navigateBack({
					 delta: 1,
				})
			},
			open(){
				this.$refs.popup.open()
			},
			close(){
				this.$refs.popup.close()
				this.addzu()
			},
		}
	}
</script>

<style>
@import url(edit.css);
</style>
